27B - Tournament - CodeForces Solution


bitmasks brute force dfs and similar greedy *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>  //  --> PBDS
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds; 
using ll = long long;
using ull = unsigned long long;

const int mod = 1e9+7;
const int mod2 = 998244353;

#define what_is(x) cerr << #x << " is " << x << endl;
#define prec(n) fixed << setprecision(n)
#define inf (ll)1e18
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define trail(x) __builtin_ctzll(x) // -> number of trailing zeros
#define pll pair<ll,ll>
#define mp make_pair
#define fr first
#define sc second
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define nl "\n"
#define lb lower_bound
#define ub upper_bound
#define oset tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> // (*s.find_by_order)(s.order_of_key)(insert,erase,size,lb,ub)
/*------------------------------------------------------------------------------------------------------------*/
ll countSetBits(ll n) { ll count = 0; while (n) { count += n & 1; n >>= 1;} return count; }
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
bool powerOfTwo(ll n) { return n && (!(n & (n-1))); }
void toLower(string& s) { transform(s.begin(), s.end(), s.begin(), ::tolower); }
void toUpper(string& s) { transform(s.begin(), s.end(), s.begin(), ::toupper); }
/*************************************************************************************************************/

vector<ll> primes; // all the primes till (1e7)
vector<bool> seive(1e7+10,true);
void createSeive()
{
    seive[0] = seive[1] = false;
    for(int i=2;i*i<=int(1e7);++i){
        if(seive[i])
        {
            for(int j=i*i;j<=int(1e7);j+=i) seive[j]=false;
        }
    }
    for(int i=0;i<seive.size();++i) if(seive[i]) primes.pb(i);
}

ll binexpo(ll a, ll b, ll m) {
    a %= m;
    ll res = 1;
    while (b > 0) {
        if (b & 1)
        res = (res * a) % m;
        a = (a * a) % m;
        b >>= 1;
    }
    return res;
}
ll modInverse(ll n, ll mod)  // euler's totient --> eulers theorm --> fermat's
{
    return binexpo(n, mod - 2, mod);  // MMI of A is --> A^(mod-2) % mod
}
vector<ll>fact;
void factorial(ll n,ll mod)
{
    fact.resize(n+1);
    fact[0]=1;
    for(int i=1;i<=n;++i) fact[i] = (fact[i-1]*i) % mod;
}
ll nCrModPFermat(ll n, ll r, ll mod)  // nCr % mod calculation
{
    fact.resize(n+1);
    if (n < r) return 0;  // If n<r, then nCr should return 0
    if (r == 0) return 1;  // Base case
    fact[0] = 1;
    for (int i = 1; i <= n; i++) fact[i] = (fact[i - 1] * i) % mod;
    return (fact[n] * modInverse(fact[r], mod) % mod* modInverse(fact[n - r], mod) % mod)% mod;
}

bool isPrime(ll n)
{
    // Corner case
    if (n <= 1) return false;
    // Check from 2 to square root of n
    for (int i = 2; i*i <= n; i++)
        if (n % i == 0) return false;
    return true;
}

class DSU{
    vector<ll> parent;
    vector<ll> subtree_size;
    ll N;
public:
    DSU(ll n){
        N = n;
        parent = vector<ll>(n+1, 0);
        subtree_size = vector<ll>(n+1, 1);

        for(int i=1; i<=n; i++) parent[i] = i;
    }

    ll findRoot(ll u){
        while(u != parent[u]){
            parent[u] = parent[parent[u]]; //Path compression
            u = parent[u];
        }

        return u;
    }

    bool combine(ll u, ll v){
        ll ru = findRoot(u); // root of u
        ll rv = findRoot(v); // root of v

        if(ru == rv) return false; // no need to join(same group)
        
        // small to large merging --> (union by rank or size)
        if(subtree_size[ru] > subtree_size[rv]){
            parent[rv] = ru;
            subtree_size[ru] += subtree_size[rv];
        } else{
            parent[ru] = rv;
            subtree_size[rv] += subtree_size[ru];
        }
        return true;
    }
};
/********************* KMP (pattern matching)*********************/
ll kmp(string String, string pattern) {
  ll i = 0, j = 0, m = pattern.length(), n = String.length();
  pattern = '#' + pattern; //just shifting the pattern indices by 1
  vector < ll > piTable(m + 1, 0);
  for (int i = 2; i <= m; i++) {
    while (j <= m && pattern[j + 1] == pattern[i])
      piTable[i++] = ++j;
    j = 0;
  }
  j = 0;
  for (int i = 0; i < n; i++) {
    if (pattern[j + 1] != String[i]) {
      while (j != 0 && pattern[j + 1] != String[i])
        j = piTable[j];
    }
    j++;
    if (j == m) return (i - m + 1); // index of the pattern in the string
  }
  return -1; // not found
}
/******************************************************************/
void precompute()
{
    
}

void solve()
{
    ll n; cin >> n;
    set<pair<ll,ll>>sp;
    map<ll,ll>mp;
    ll k = (n * (n-1) )/2 - 1;
    for (ll i = 0; i < k ; ++i)
    {
        ll x,y; cin >> x >> y;
        mp[x]++;
        sp.insert({x,y});
        sp.insert({y,x});
    }
    
    set<pair<ll,ll>> sp2;
    for (ll i = 1; i <=n ; ++i)
    {
        for (ll j = i+1; j <=n; ++j)
        {
            sp2.insert({i,j});
            sp2.insert({j,i});
        }
    }
    
    
    pair<ll,ll> nhihua;
    ll one,second;
    for(auto pr : sp2)
    {
        if(sp.find(pr) == sp.end())
        {
            one = pr.fr;
            second = pr.sc;
            break;
        }
    }
    
    
    if(mp[one] > mp[second])
    {
        cout << one << " " << second << endl;
    }else{
        cout << second << " " << one << endl;
    }
    
    
    
    
    
    
    
    
    
}

signed main() {
    
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // fast io
    precompute();
    int tc = 1;
    // cin >> tc;
    for(int t=1;t<=tc;++t)
    {
        //cout << "Case #" << i << ": ";
        solve();
    } 
return 0;  
}
//end
  		 		    				 			     	  	


Comments

Submit
0 Comments
More Questions

1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite